package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.MunicipalFacilities;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.TraSysLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/14
 * @Description: 通信系统设施、设备地震损失表
 */
//@Service("交通系统设施、设备地震损失表")
@Service("4")
public class TraSysLossServiceImpl implements StrategyService {
    @Resource
    TraSysLossRepository traSysLossRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;
    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            traSysLossRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        List<MunicipalFacilities> list = getList(workbook, xlrmId);
        traSysLossRepository.batchSave(list);
        reportPublicField.setProvince(list.get(0).getProvince());
        reportPublicField.setCity(list.get(0).getCity());
        reportPublicField.setCounty(list.get(0).getCounty());
        reportPublicField.setTownship(list.get(0).getTownship());
        reportPublicField.setEnterpriseName(list.get(0).getEnterpriseName());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }
    //获取list
    private List<MunicipalFacilities> getList(Workbook workbook, String xlrmId) {
        //构筑物部分
        List<MunicipalFacilities> list = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String district = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //乡
            String township = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(7));
            //分项
            String subitem = null;
            //损失合计
            String totalLoss = String.valueOf(sheet.getRow(totalRows-1).getCell(12).getNumericCellValue());
            /** 循环Excel的行 从第七行开始*/
            for (int r = 6; r < totalRows-1; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                MunicipalFacilities municipal= new MunicipalFacilities();
                //主键id
                municipal.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                //报表主表id
                municipal.setXlrmId(xlrmId);
                //填表人
                municipal.setFillFormUser(fillFormUser);
                //tel
                municipal.setTel(tel);
                //填报日期
                municipal.setFillFormTime(fillFormTime);
                //省
                municipal.setProvince(province);
                //市
                municipal.setCity(city);
                //县
                municipal.setCounty(district);
                //乡
                municipal.setTownship(township);
                if (!hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("")){
                    subitem = hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                //分项
                municipal.setSubitem(subitem);
                //设施
                municipal.setFacility(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                //等级规模
                municipal.setGradeScale(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                //原值(万元)
                municipal.setOriginalValue(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                //地点
                municipal.setPlace(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                //破坏情况描述
                municipal.setDamageConditionDesc(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                //损坏
                municipal.setDamage(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                //破坏
                municipal.setDestroy(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                //毁坏
                municipal.setRuined(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                //应急抢修情况或建议
                municipal.setUrgentRepair(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                //估算依据
                municipal.setEstimationReliedOn(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                //估计损失(万元)
                municipal.setEstimationLoss(hSSFDataFormatter.formatCellValue(row.getCell(12)));
                //备注
                municipal.setComment(hSSFDataFormatter.formatCellValue(row.getCell(13)));
                //损失合计
                municipal.setTotalLoss(totalLoss);
                //顺序
                municipal.setSequence(String.valueOf(r));
                list.add(municipal);
            }
        }
        return list;
    }
}
